capture program drop storeresult
program define storeresult
*! storeresult - Regression results storage and output.
*! Nicolas Van de Sijpe 28-May-2017
/* "storeresult" is a program to capture results from our analysis and output them, 
including all relevant statistics for each estimation technique.
The syntax is simply: storeresult place-to-store-estimates */
version 14
syntax namelist (min=1 max=1)


*************************************************************************************
* SET UP LOCAL MACROS AND TEMPORARY NAMES TO BE USED BY THE PROGRAM
*************************************************************************************
local esttechn `e(cmd)' // Picks up estimation technique
local resultsstorage: word 1 of `namelist' // Picks up the place where to store results
local depvar `e(depvar)' // Picks up the dependent variable
local RHSincl: colnames e(b) // Picks up all RHS variables, including the constant, time dummies...


*************************************************************************************
* IDENTIFY TYPE OF ARDL MODEL THAT IS BEING ESTIMATED
*************************************************************************************
local lagdep lagdepvar
local AR1: list lagdep in RHSincl // AR1 is 1 for a model that includes first lag of dep var (lagdep), 0 otherwise 
local aidvar aidvar
local aidt: list aidvar in RHSincl // aidt is 1 for a model that includes current aid variable (aidvar), 0 otherwise 
local lagaidvar lagaidvar
local aidtmin1: list lagaidvar in RHSincl // aidtmin1 is 1 for a model that includes one period lagged aid variable (lagaidvar), 0 otherwise 


*********************************************************************************
* CREATE INDICATOR VARIABLE FOR SAMPLE
*********************************************************************************
tempvar sample
capture drop `sample'
gen `sample' = 1 if e(sample) // tempvar sample indicator


*******************************************************************************************
* DISPLAY ERROR MESSAGE IF RESULTSSTORAGE DISAGREES WITH LAST PLACE YOU STORED ESTIMATES 
*******************************************************************************************
if "`resultsstorage'" != "`e(_estimates_name)'" {
	di as error "You're not storing the results in the right place"
	exit 198
}


/* REST OF THE CODE PICKS UP ESTIMATOR-SPECIFIC STATISTICS. */


************************************
* XTREG (FE, CCE)
************************************
if "`esttechn'" == "xtreg" { /* XTREG ESTIMATOR */
	local XTREGesttype `e(model)'
	if "`XTREGesttype'" == "fe" { /* FE estimator */
		
		estadd scalar countries = e(N_g): `resultsstorage'

		/* If model contains any dynamics, also save results for the long-run effect. */
		if `AR1' == 1 & `aidt' == 1 { // AR1 model with current aid

			/* Calculate LR effect and carry out test for LR effect. */
			nlcom (LR: _b[aidvar]/(1-_b[lagdepvar])), post
			scalar define LReffect = _b[LR]
			estadd scalar LReffect: `resultsstorage' 
			scalar define LReffect_se = _se[LR]
			estadd scalar LReffect_se: `resultsstorage' 
			test _b[LR] = 0
			estadd scalar LReffect_p = r(p): `resultsstorage'			
			
		}/* end code for AR1 model with current aid. */
	}/* end code FE estimator */
	else {
		di as error "Something is wrong with the XTREG estimation type: you're not estimating a FE model."
		exit 198
	}/* end code XTREG estimation type misspecified */
} /* END XTREG ESTIMATOR */


************************************
* XTIVREG2 (FE IV, CCE IV)
************************************
else if "`esttechn'" == "xtivreg2" { /* XTIVREG2 ESTIMATOR */
	local XTIVREG2esttype `e(xtmodel)'
	if "`XTIVREG2esttype'" == "fe" { /* FE estimator */

		estadd scalar countries = e(N_g): `resultsstorage'
		estadd scalar underid_p = e(idp): `resultsstorage'
		estadd scalar underid_stat = e(idstat): `resultsstorage'
		estadd scalar weakid_Fstat = e(widstat): `resultsstorage' // F statistic for weak identification

		/* If model contains any dynamics, also save results for the long-run effect. */
		if `AR1' == 1 & `aidt' == 1 & `aidtmin1' == 0 { // AR1 model with current aid only
	
			/* Calculate LR effect and carry out test for LR effect. */
			nlcom (LR: _b[aidvar]/(1-_b[lagdepvar])), post
			scalar define LReffect = _b[LR]
			estadd scalar LReffect: `resultsstorage' 
			scalar define LReffect_se = _se[LR]
			estadd scalar LReffect_se: `resultsstorage' 
			test _b[LR] = 0
			estadd scalar LReffect_p = r(p): `resultsstorage'			
			
		}/* end code for AR1 model with current aid only. */
		else if `AR1' == 1 & `aidt' == 0 & `aidtmin1' == 1 { // AR1 model with one period lagged aid only.
	
			/* Calculate LR effect and carry out test for LR effect. */
			nlcom (LR: _b[lagaidvar]/(1-_b[lagdepvar])), post
			scalar define LReffect = _b[LR]
			estadd scalar LReffect: `resultsstorage' 
			scalar define LReffect_se = _se[LR]
			estadd scalar LReffect_se: `resultsstorage' 
			test _b[LR] = 0
			estadd scalar LReffect_p = r(p): `resultsstorage'
			
		}/* end code for AR1 model with one period lagged aid only. */
		else if `AR1' == 0 & `aidt' == 1 & `aidtmin1' == 1 { // AR(0) model with current and one period lagged aid.
	
			/* Calculate LR effect and carry out test for LR effect. */
			nlcom (LR: _b[lagaidvar] + _b[aidvar]), post
			scalar define LReffect = _b[LR]
			estadd scalar LReffect: `resultsstorage' 
			scalar define LReffect_se = _se[LR]
			estadd scalar LReffect_se: `resultsstorage' 
			test _b[LR] = 0
			estadd scalar LReffect_p = r(p): `resultsstorage'			
			
		}/* end code for AR(0) model with current and one period lagged aid. */
		else if `AR1' == 1 & `aidt' == 1 & `aidtmin1' == 1 { // AR(1) model with current and one period lagged aid.
	
			/* Calculate LR effect and carry out test for LR effect. */
			nlcom (LR: (_b[lagaidvar] + _b[aidvar])/(1-_b[lagdepvar])), post
			scalar define LReffect = _b[LR]
			estadd scalar LReffect: `resultsstorage' 
			scalar define LReffect_se = _se[LR]
			estadd scalar LReffect_se: `resultsstorage' 
			test _b[LR] = 0
			estadd scalar LReffect_p = r(p): `resultsstorage'			
			
		}/* end code for AR(1) model with current and one period lagged aid. */
	}/* end code FE IV estimator */
	else {
		di as error "Something is wrong with the XTIVREG2 estimation type: you're not estimating a FE model."
		exit 198
	}/* end code XTIVREG2 estimation type misspecified */
} /* END XTIVREG2 ESTIMATOR */


************************************
* IVREG2 (CCE IV without FEs)
************************************
else if "`esttechn'" == "ivreg2" { /* IVREG2 ESTIMATOR */

	estadd scalar countries = e(N_clust): `resultsstorage' // assumes SEs are clustered.
	estadd scalar underid_p = e(idp): `resultsstorage'
	estadd scalar underid_stat = e(idstat): `resultsstorage'
	estadd scalar weakid_Fstat = e(widstat): `resultsstorage' // F statistic for weak identification
	
	/* If model contains any dynamics, also save results for the long-run effect. */
	if `AR1' == 1 & `aidt' == 1 & `aidtmin1' == 0 { // AR1 model with current aid only
		/* Calculate LR effect and carry out test for LR effect. */
		nlcom (LR: _b[aidvar]/(1-_b[lagdepvar])), post
		scalar define LReffect = _b[LR]
		estadd scalar LReffect: `resultsstorage' 
		scalar define LReffect_se = _se[LR]
		estadd scalar LReffect_se: `resultsstorage' 
		test _b[LR] = 0
		estadd scalar LReffect_p = r(p): `resultsstorage'			
	}/* end code for AR1 model with current aid only. */
} /* END IVREG2 ESTIMATOR */


************************************************
* ESTIMATION TECHNIQUE CANNOT BE DEALT WITH
************************************************
else { /* Estimation technique not recognised */
	di as error "storeresult cannot deal with the estimator used"
	exit 198
} /* End code estimation technique not recognised */


************************************************
* DROP SAMPLE INDICATOR VARIABLE
************************************************
capture drop `sample'


************************************************
* END PROGRAM
************************************************
end
